package com.dingwei.superiordiary.mvp.presenter;

import android.text.TextUtils;

import com.dingwei.superiordiary.base.MyBaseBean;
import com.dingwei.superiordiary.bean.FailsBean;
import com.dingwei.superiordiary.bean.OrderPayBean;
import com.dingwei.superiordiary.custom.myCallback;
import com.dingwei.superiordiary.mvp.view.Contract;
import com.lihao.baselibrary.base.BaseActivity;
import com.lihao.baselibrary.base.URLS;
import com.lihao.baselibrary.base.basePresenter;

import org.xutils.http.RequestParams;
import org.xutils.x;

/**
 * Created by lihao on 2017/9/12.
 */

public class FirmOrderPresenter extends basePresenter {

    private Contract.FirmOrderView firmOrderView;

    public FirmOrderPresenter(Contract.FirmOrderView view) {
        super(view);
        this.firmOrderView = view;
    }

    public void payOrder(String id, int num, double freight, double price, String attribute, String address_id, String remark, final String payTag,String share_key) {

        if (TextUtils.isEmpty(address_id)){
            showToast("请选择收货地址");
            return;
        }

        RequestParams rp = new RequestParams(URLS.HOST + URLS.PAY_ORDER);
        rp.addBodyParameter("sessionid", BaseActivity.value.getToken());
        rp.addBodyParameter("id", id);
        rp.addBodyParameter("quantity", num + "");
        rp.addBodyParameter("freight", freight + "");
        rp.addBodyParameter("price", price + "");
        if (!TextUtils.isEmpty(attribute)){
            rp.addBodyParameter("option", attribute);
        }
        rp.addBodyParameter("address_id", address_id );
        rp.addBodyParameter("remark", remark);
        if (!TextUtils.isEmpty(share_key)) {
            rp.addBodyParameter("share_key",share_key);
        }

        x.http().post(rp, new myCallback(BaseActivity.context, OrderPayBean.class, rp) {
            @Override
            public void back(MyBaseBean baseBean, String result) {
                OrderPayBean bean = (OrderPayBean) baseBean;
                if (payTag.equals("wx")){
                    firmOrderView.wxPay(bean);
                }else if (payTag.equals("alipay")){
                    firmOrderView.alipay(bean);
                }else {
                    firmOrderView.exchangeSuccess(bean);
                }
            }

            @Override
            public void fail(FailsBean baseBean, String result) {
            }
        });
    }
}
